<template>
	<div class="todo-container">
    <div class="todo-wrap">
      <Header @add-todo="addTodo"/>
      <List :todos="todos" :updateTodo="updateTodo" :delteTodo="delteTodo"/>
      <Footer :todos="todos" :clearAllDone="clearAllDone" :updateAll="updateAll"/>
			<button @click="a.c.d.e = 900">点我修改a</button>
    </div>
  </div>
</template>

<script>
	import Header from './components/Header'
	import List from './components/List'
	import Footer from './components/Footer'

	export default {
		name:'App',
		components:{Header,List,Footer},
		data(){
			return {
				//保存一堆要做的事
				todos:[
					{id:'001',title:'抽烟',done:true},
					{id:'002',title:'喝酒',done:false},
					{id:'003',title:'开车',done:true}
				],
				a:{b:1,c:{d:{e:2}}}
			}
		},
		methods:{
			//用于给App的data中添加一个todo对象
			addTodo(todoObj){
				this.todos.unshift(todoObj)
			},

			//更新一个todo ------ 用id去更新
			/* updateTodo(id,done){
				this.todos.forEach((todoObj)=>{
					if(todoObj.id === id) todoObj.done = done
				})
			} */

			//更新一个todo ------ 用index去更新
			updateTodo(index,done){
				this.todos[index].done = done
			},

			//删除一个todo----用index去删除
			delteTodo(index){
				this.todos.splice(index,1)
			},

			//删除所有已完成
			clearAllDone(){
				const newTodos = this.todos.filter((todoObj)=>{
					return !todoObj.done
				})
				this.todos = newTodos
			},

			//更新所有
			updateAll(done){
				// console.log('App',done)
				this.todos.forEach((todoObj)=>{
					todoObj.done = done
				})
			}
		},
		watch:{
			todos:{
				deep:true, //开启深度监视
				handler(){
					console.log('todos被别人修改了')
				}
			}
		}
	}
</script>

<style>
	/*base*/
	body {
		background: #fff;
	}

	.btn {
		display: inline-block;
		padding: 4px 12px;
		margin-bottom: 0;
		font-size: 14px;
		line-height: 20px;
		text-align: center;
		vertical-align: middle;
		cursor: pointer;
		box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
		border-radius: 4px;
	}

	.btn-danger {
		color: #fff;
		background-color: #da4f49;
		border: 1px solid #bd362f;
	}

	.btn-danger:hover {
		color: #fff;
		background-color: #bd362f;
	}

	.btn:focus {
		outline: none;
	}

	.todo-container {
		width: 600px;
		margin: 0 auto;
	}
	.todo-container .todo-wrap {
		padding: 10px;
		border: 1px solid #ddd;
		border-radius: 5px;
	}
</style>